package com.wjl.guli.service.edu.controller.admin;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wjl.guli.common.base.R;
import com.wjl.guli.service.edu.entity.Teacher;
import com.wjl.guli.service.edu.entity.vo.TeacherVo;
import com.wjl.guli.service.edu.feign.OssFileService;
import com.wjl.guli.service.edu.service.TeacherService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author wjl
 * @since 2020-11-24
 */
@Api(tags = "Teacher信息管理")
@RestController
@RequestMapping("/admin/edu/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private OssFileService ossFileService;

    @ApiOperation("查询所有数据")
    @GetMapping("list")
    public R listAll(){
        List<Teacher> list = teacherService.list();
        return R.ok().data("items",list);
    }
    @ApiOperation("根据id删除")
    @DeleteMapping("remove")
    public R deleteById(@ApiParam("记录id值") Long id){

        boolean b1 = teacherService.removeAvatarById(id);
        System.out.println("-------------------------"+b1);
        boolean b = teacherService.removeById(id);

       if (b){
           return R.ok().message("删除成功");
       }
       else {
           return R.error().message("没有该数据");
       }
   }
    @ApiOperation("分页查询")
    @GetMapping("listPage")
   public R listPage(@ApiParam(value = "当前页",required = true) Long current,@ApiParam(value = "每页显示的记录条数",required = true) Long size,
                     @ApiParam(value = "教师查询对象")String teacherVo
    ){
        TeacherVo teacherVo1 = JSONObject.parseObject(teacherVo,TeacherVo.class);
       Page page=new Page(current,size);
       IPage page1 = teacherService.pageQuery(page,teacherVo1);
       List records = page1.getRecords();
       long total = page1.getTotal();
       return R.ok().data("records",records).data("total",total);
   }
    @ApiOperation("添加教师 ")
    @PostMapping("add")
   public R addT(@ApiParam("添加的Teacher对象") @RequestBody Teacher teacher){

       boolean save = teacherService.save(teacher);
       if (save){
           return R.ok().message("添加成功");
       }
       return R.error().message("添加失败");
   }

    @ApiOperation("修改教师")
    @PutMapping("update")
    public R editT(@ApiParam("修改的Teacher对象") @RequestBody Teacher teacher){
        System.out.println(teacher.toString());
        boolean b = teacherService.updateById(teacher);
        if (b){
            return R.ok().message("更新成功");
        }
        return R.error().message("更新失败");
    }
    @ApiOperation("根据id查询")
    @GetMapping("findById/{id}")
    public R findById(@ApiParam(value = "对象ID",required = true) @PathVariable Long id){
        Teacher byId = teacherService.getById(id);
        if (byId!=null){
            return R.ok().data("item",byId);
        }
        return R.error().message("没有该记录");
    }
    @ApiOperation("根据id删除")
    @DeleteMapping("removeSome")
    public R deleteByIds(@ApiParam("记录id数组") @RequestBody List<Long> ids){
        boolean b = teacherService.removeByIds(ids);
        if (b){
            return R.ok().message("删除成功");
        }
        else {
            return R.error().message("没有该数据");
        }
    }


    @ApiOperation("根据key查询教师列表")
    @GetMapping("list/name/{key}")
    public R selectNameListByKey(
            @ApiParam(value = "关键字",required = true)
            @PathVariable
            String key
    ){
        List<Map<String,Object>> list = teacherService.selectNameListByKey(key);
        return R.ok().data("items",list);
    }





    @ApiOperation("Oss测试")
    @GetMapping("test")
    public R testOss(){
        return ossFileService.test();

    }
}

